﻿Imports Microsoft.VisualBasic
Imports System.Collections.Generic
Imports System.Linq
Namespace OneCall.BO
    Public Class UserAuth
        Dim reqpage As System.Web.HttpRequest
        Dim Req As System.Web.HttpRequest
        Dim Res As System.Web.HttpResponse
        Public Sub New()

        End Sub
        Public Sub New(ByVal mpage As System.Web.HttpRequest)
            reqpage = mpage
        End Sub
        Public Sub New(ByVal Request As HttpRequest, ByVal Response As HttpResponse)
            Res = Response
            Req = Request
        End Sub
        Public Function GetUserName() As String
            If isActiveSession(reqpage) Then
                Dim key As String = Req.Cookies("1CBOSESSON")("k")
                Dim uid As Integer = OneCall.Crypt.Decrypt(Req.Cookies("1CBOSESSON")("_uid_"), key)
                Dim SQLDB As New OneCall.DataModel.OneCDataEntities
                Dim useList As List(Of OneCall.DataModel.User) = (From p In SQLDB.Users Where p.User_ID = uid Select p).ToList()
                If useList.Count > 0 Then
                    Dim fname As String = useList(0).User_FirstName & " " & useList(0).User_LastName
                    Return fname
                Else
                    Return ""
                End If
                SQLDB.Dispose()
            Else
                Return ""
            End If
        End Function
        Public Sub RegisterUserSession(ByVal mpage As System.Web.HttpResponse, ByVal UserID As Integer, ByVal remme As Boolean)
            Dim sessonCookie As New HttpCookie("1CBOSESSON")
            Dim key As String = Guid.NewGuid().ToString()
            sessonCookie.Values("k") = key
            sessonCookie.Values("_uid_") = OneCall.Crypt.Encrypt(UserID, key)
            sessonCookie.Values("_ssn_") = OneCall.Crypt.Encrypt("Active", key)
            sessonCookie.Expires = Now().AddHours(1)
            mpage.Cookies.Add(sessonCookie)
            mpage.Redirect("main.aspx")

        End Sub
        Public Function isActiveSession(ByVal mpage As System.Web.HttpRequest) As Boolean
            If Not Req.Cookies("1CBOSESSON") Is Nothing Then
                Dim key As String = Req.Cookies("1CBOSESSON")("k")
                If Req.Cookies("1CBOSESSON")("_ssn_") = OneCall.Crypt.Encrypt("Active", key) Then
                    Return True
                Else
                    Return False
                End If
            Else
                Return False
            End If
        End Function
        Public Function GetActiveUserID(ByVal mpage As System.Web.HttpRequest) As Integer
            If isActiveSession(mpage) Then
                Dim key As String = Req.Cookies("1CBOSESSON")("k")
                Return Int(OneCall.Crypt.Decrypt(mpage.Cookies("1CBOSESSON")("User_ID"), key))
            Else
                Return 0
            End If
        End Function
        Public Sub SignOut(ByVal mpage As System.Web.HttpRequest)
            If isActiveSession(mpage) Then
                Dim key As String = Req.Cookies("1CBOSESSON")("k")
                Res.Cookies("1CBOSESSON")("_ssn_") = ""
                Res.Cookies("1CBOSESSON").Values.Clear()
                Res.Cookies("1CBOSESSON").Expires = Now().AddDays(-1)
            End If
        End Sub
    End Class

End Namespace

